package com.example.cupid.controller;

import com.example.cupid.pojo.User;
import com.example.cupid.pojo.Wechat;
import com.example.cupid.service.Impl.TagServiceImpl;
import com.example.cupid.service.Impl.WechatServiceImpl;
import com.example.cupid.service.TagService;
import com.example.cupid.util.MsgUtil;
import com.sun.scenario.effect.impl.sw.java.JSWBrightpassPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 2021/9/3
 **/
@Controller
@RequestMapping("/wechat")
public class WechatController {

    @Autowired
    private WechatServiceImpl wechatService;

    @Autowired
    private TagServiceImpl tagService;

    @ResponseBody
    @GetMapping("/checkWechat")
    public boolean checkWechat(String wechat){
        System.out.println(wechat);
        int count = wechatService.checkWechat(wechat);
        return count!=0;
    }
    @GetMapping("/manage")
    public String toWechatManage(@RequestParam int id, Model model){
        List<Wechat> saveWechats = wechatService.getSaveWechatByUserId(id);
        List<Wechat> takeWechats = wechatService.getTakeWechatByUserId(id);
        for (Wechat wechat : saveWechats) {
            List<String> tagNames = wechatService.getTagNamesByWechatId(wechat.getId());
            wechat.setTagNames(tagNames);
        }
        for (Wechat wechat : takeWechats) {
            List<String> tagNames = wechatService.getTagNamesByWechatId(wechat.getId());
            wechat.setTagNames(tagNames);
        }
        model.addAttribute("saveWechats",saveWechats);
        model.addAttribute("takeWechats",takeWechats);
        return "wechat_manage";
    }
    @GetMapping("/success")
    public String toSuccess(){
        return "success";
    }

//    指定获取微信
    @PostMapping("/signalTake")
    public String signalTake( String msg, HttpServletRequest request){
        Wechat wechat = MsgUtil.msgToWechat(msg);
        Integer userId = MsgUtil.msgToUserId(msg);
        System.out.println("wechat======"+wechat);
        List<Wechat> wechats = wechatService.getWechatsAccordWithUser(wechat,userId);
        HttpSession session = request.getSession();
        session.setAttribute("wechats",wechats);
        session.setAttribute("action","signal");
        return "redirect:/wechat/takeResult";
    }
    @PostMapping("randomTake")
    public String randomTake(String msg, HttpServletRequest request){
        int sex = Integer.parseInt(msg.substring(msg.length()-1));
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        Wechat wechat = wechatService.getRandomWechatBySex(loginUser.getId(),sex);
        wechatService.saveUserWechatTake(loginUser.getId(),wechat.getId());
        ArrayList<Wechat> wechats = new ArrayList<>();
        wechats.add(wechat);
        session.setAttribute("wechats",wechats);
        session.setAttribute("action","random");
        return "redirect:/wechat/takeResult";
    }
//      重定向查看获取结果，防止刷新
    @GetMapping("/takeResult")
    public String takeResult(Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        model.addAttribute("wechats",session.getAttribute("wechats"));
        model.addAttribute("action",session.getAttribute("action"));
        return "take_result";
    }


//    保存获取的微信
    @PostMapping("/saveTake")
    @ResponseBody
    public boolean saveTake(HttpServletRequest request,String wechatId){
        HttpSession session = request.getSession();
        User loginUser =(User) session.getAttribute("loginUser");
        wechatService.saveUserWechatTake(loginUser.getId(),Integer.parseInt(wechatId));
        return true;
    }
//    随机获取时  首先检查数据库中是否存在数据
    @GetMapping("/randomCheck")
    @ResponseBody
    public boolean randomCheck(String sex,HttpServletRequest request){
        HttpSession session = request.getSession();
        User loginUser =(User) session.getAttribute("loginUser");
        int sexInt = Integer.parseInt(sex);
        Wechat wechat = wechatService.getRandomWechatBySex(loginUser.getId(), sexInt);
        return wechat != null;
    }
    //    指定获取时  首先检查数据库中是否存在数据
    @GetMapping("/signalCheck")
    @ResponseBody
    public Integer signalCheck(Wechat wechat,HttpServletRequest request){
        HttpSession session = request.getSession();
        User loginUser =(User) session.getAttribute("loginUser");
        return wechatService.signalCheck(loginUser.getId(),wechat);
    }
}
